package com.network.action;

import java.util.ArrayList;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.network.cfg.Configuration;
import com.network.entity.Page;
import com.network.entity.User;
import com.network.entity.UserQryHelper;
import com.network.service.IUserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author Ling xiaowei
 *
 */
@Component
public class AdminAction extends ActionSupport {
	
	private User user;
	
	private UserQryHelper helper;
	
	private String username;
	
	private String password;
	
	private IUserService userService;
	
	private ArrayList<User> userArr;
	
	public String gotoAddUser()
	{
		return SUCCESS;
	}
	
	public String addUser()
	{
		String name = user.getUserName();
		if(!userService.isUsernameExisted(name))
		{
			user.setCreateDate(new Date());
			userService.saveOrUpdateUser(user);
			ActionContext.getContext().put(Configuration.ADMIN_RETURN_MSG, "用户添加成功！");
			return SUCCESS;
		}
		else
		{
			//用户名已经存在，用户添加失败
			ActionContext.getContext().put(Configuration.ADMIN_RETURN_MSG, "用户添加失败！用户名已存在！");
			return ERROR;
		}
	}
	
	public String gotoSearchUser()
	{
		return SUCCESS;
	}
	
	public String modifyUserInfo()
	{
		userService.saveOrUpdateUser(user);
		ActionContext.getContext().put(Configuration.ADMIN_RETURN_MSG, "用户信息修改成功！");
		return SUCCESS;
	}
	
	public String modifyPassword()
	{
		user = userService.getUserById(user.getUserName());
		user.setPassword(password);
		userService.saveOrUpdateUser(user);
		ActionContext.getContext().put(Configuration.ADMIN_RETURN_MSG, "用户密码修改成功！");
		return SUCCESS;
	}
	
	public String getUserList()
	{
		//TODO: 需要改成工厂模式获取对象
		Page p = new Page();
		helper = new UserQryHelper();
		ActionContext.getContext().getSession().put(Configuration.PAGE_OBJ, p);
		ActionContext.getContext().getSession().put(Configuration.HELPER_OBJ, helper);
		userArr = userService.qryUser(p, helper);
		return SUCCESS;
	}
	
	public String searchUser()
	{
		Page p = new Page();
		ActionContext.getContext().getSession().put(Configuration.PAGE_OBJ, p);

		ActionContext.getContext().getSession().put(Configuration.HELPER_OBJ, helper);

		userArr = userService.qryUser(p, helper);
		return SUCCESS;
	}
	
	public String getNextPage()
	{
		Page p = (Page) ActionContext.getContext().getSession().get(Configuration.PAGE_OBJ);
		if(null == p)
		{
			p = new Page();
			ActionContext.getContext().getSession().put(Configuration.PAGE_OBJ, p);
		}
		else
		{
			p.setPageNo(p.getNextPage());
		}
		UserQryHelper helper = (UserQryHelper) ActionContext.getContext().getSession().get(Configuration.HELPER_OBJ);
		if(null == helper)
		{
			helper = new UserQryHelper();
			ActionContext.getContext().getSession().put(Configuration.HELPER_OBJ, helper);
		}
		else
		{
			//do nothing
		
		}
		userArr = userService.qryUser(p, helper);
		return SUCCESS;
	}
	
	public String getPrePage()
	{
		Page p = (Page) ActionContext.getContext().getSession().get(Configuration.PAGE_OBJ);
		if(null == p)
		{
			p = new Page();
			ActionContext.getContext().getSession().put(Configuration.PAGE_OBJ, p);
		}
		else
		{
			p.setPageNo(p.getPrePage());
		}
		UserQryHelper helper = (UserQryHelper) ActionContext.getContext().getSession().get(Configuration.HELPER_OBJ);
		if(null == helper)
		{
			helper = new UserQryHelper();
			ActionContext.getContext().getSession().put(Configuration.HELPER_OBJ, helper);
		}
		else
		{
			//do nothing
		
		}
		userArr = userService.qryUser(p, helper);
		return SUCCESS;
	
	}
	
	public String getUserById()
	{
		user = userService.getUserById(username);
		if(null == user)
		{
			ActionContext.getContext().put(Configuration.ADMIN_RETURN_MSG, "没有查找到相应的用户！");
			return ERROR;
		}
		else
		{
			return SUCCESS;
		}
	}
	
	public String deleteUserById()
	{
		User u = (User) ActionContext.getContext().getSession().get(Configuration.USER);
		String loginName = u.getName();
		String role = u.getRole();
		if(role.equals(Configuration.USER_FLAG))
		{
			ActionContext.getContext().put(Configuration.ADMIN_RETURN_MSG, "删除失败！没有删除账号的权限！");
			return ERROR;
		}
		else if(loginName.equals(username))
		{
			ActionContext.getContext().put(Configuration.ADMIN_RETURN_MSG, "删除失败！不能删除自己的账号！");
			return ERROR;
		}
		else if(username.equals(Configuration.ROOT_USERNAME))
		{
			ActionContext.getContext().put(Configuration.ADMIN_RETURN_MSG, "删除失败！ROOT账号不能被删除！");
			return ERROR;
		}
		else
		{
			userService.deleteUserById(username);
			ActionContext.getContext().put(Configuration.ADMIN_RETURN_MSG, "账号删除成功！");
			return SUCCESS;
		}
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public IUserService getUserService() {
		return userService;
	}

	@Resource(name="userServiceImpl")
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public UserQryHelper getHelper() {
		return helper;
	}

	public void setHelper(UserQryHelper helper) {
		this.helper = helper;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public ArrayList<User> getUserArr() {
		return userArr;
	}

	public void setUserArr(ArrayList<User> userArr) {
		this.userArr = userArr;
	}
	
}
